import math, sys, os, random, pygame

# Define some globally used color vectors.
red = (255, 0, 0)
black = (0, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
yellow = (255, 255, 0)

class Graphics:
    """
    Graphics is a concrete class hodling the display and some miscellaneous 
    graphics-related methods.
    """
    WIDTH = 0
    HEIGHT = 0
    
    def __init__(self):
        # Register the display!
        self.background = self.makeBackground()
        self.display = pygame.display.set_mode( self.background.get_size() )
        self.display.blit( self.background, self.background.get_rect() )
        Graphics.WIDTH, Graphics.HEIGHT = self.background.get_size()

    def makeBackground(self):
        """
        Returns the background Surface.
        """
        # Load in the data and make a pygame surface of the right size.
        mapFile = open("layouts/map.dat", 'r')
        mapData = mapFile.readlines()
        size = width, height = 48*(len(mapData[0]) - 1), 48 * len(mapData)
        background = pygame.Surface(size)
        # Register the tile dictionary.
        tile = {}
        tile[1] = pygame.image.load("sprites/tiles/13.png")
        tile[2] = pygame.image.load("sprites/tiles/14.png")
        tile[3] = pygame.image.load("sprites/tiles/15.png")
        tile[4] = pygame.image.load("sprites/tiles/23.png")
        # We will use a random middle tile -- see below.
        tile[6] = pygame.image.load("sprites/tiles/25.png")
        tile[7] = pygame.image.load("sprites/tiles/33.png")
        tile[8] = pygame.image.load("sprites/tiles/34.png")
        tile[9] = pygame.image.load("sprites/tiles/35.png")
        # Randomize these for a less repeating ground.
        tile[10] = pygame.image.load("sprites/tiles/16.png")
        tile[11] = pygame.image.load("sprites/tiles/17.png")
        tile[12] = pygame.image.load("sprites/tiles/26.png")
        tile[13] = pygame.image.load("sprites/tiles/27.png")
        # Blit the tiles onto the background, and return it.
        tempRect = tile[10].get_rect()
        for i in range(len(mapData)):
            for j in range(len(mapData[0]) - 1):
                tempRect.topleft = (j*48, i*48)
                if int(mapData[i][j]) == 5:
                    background.blit(tile[random.randint(10,13)], tempRect)
                else:
                    background.blit(tile[int(mapData[i][j])], tempRect)
        return background
                    
#################################
# Some general graphics methods #
#################################

def loadImage(path):
    """
    Returns image converted to pixel-type of display (by default).
    """
    image = pygame.image.load(path)
    if image.get_alpha() is None:
        return image.convert()
    else:
        return image.convert_alpha()

def loadSprites(path):
    """
    Returns a list of loaded images from path sorted by filename.
    """
    names = []
    sprites = []
    for f in os.listdir(path):
        names.append(f)
    names.sort()
    for n in names:
        sprites.append( loadImage(path + n) )
    return sprites


